package com.qmx.qmxapp.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;

import com.example.zhouwei.library.CustomPopWindow;
import com.qmx.qmxapp.R;
import com.qmx.qmxapp.base.BaseActivity;
import com.qmx.qmxapp.fragment.CompletedMeetingFragment;
import com.qmx.qmxapp.fragment.PendingParticipationFragment;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author ChengXiangtong
 * @create 2018/10/29
 * @Describe 我的日程
 */

public class MyScheduleActivity extends BaseActivity {

    private static final String[] CHANNELS = new String[]{"待参加", "已结束"};
    private List<String> mDataList = Arrays.asList(CHANNELS);
    private ViewPager mViewPager;
    private List<Fragment> mFragments = new ArrayList<Fragment>();
    private MagicIndicator magicIndicator;
    private MyAdapter myAdapter;

    @BindView(R.id.iv_add)
    ImageView iv_add;

    private CustomPopWindow mCustomPopWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_schedule);
        ButterKnife.bind(this);
        mViewPager = findViewById(R.id.view_pager);
        myAdapter = new MyAdapter(getSupportFragmentManager(), mFragments);
        mViewPager.setAdapter(myAdapter);
        initFragments();
        initMagicIndicator();

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                magicIndicator.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                magicIndicator.onPageSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                magicIndicator.onPageScrollStateChanged(state);
            }
        });

    }

    private void initFragments() {
        PendingParticipationFragment pendingParticipationFragment = new PendingParticipationFragment();
        mFragments.add(pendingParticipationFragment);
        CompletedMeetingFragment completedMeetingFragment = new CompletedMeetingFragment();
        mFragments.add(completedMeetingFragment);

    }

    @SuppressLint("ResourceAsColor")
    private void initMagicIndicator() {
        magicIndicator = findViewById(R.id.magic_indicator);
        //        magicIndicator.setBackgroundColor(R.color.colorBg);
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int i) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setText(mDataList.get(i));
                simplePagerTitleView.setNormalColor(Color.WHITE);
                simplePagerTitleView.setSelectedColor(Color.parseColor("#FF42ADFF"));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mViewPager.setCurrentItem(i);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setStartInterpolator(new AccelerateInterpolator());
                indicator.setEndInterpolator(new DecelerateInterpolator(1.6f));
                indicator.setYOffset(UIUtil.dip2px(context, 0));
                indicator.setLineHeight(UIUtil.dip2px(context, 1));
                indicator.setColors(Color.parseColor("#FF42ADFF"));
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, mViewPager);

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
    }

    @OnClick(R.id.iv_add)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_add:
                showPopMenu();
                break;
        }
    }


    private void showPopMenu() {
        View contentView = LayoutInflater.from(this).inflate(R.layout.pop_meeting_menu, null);
        //处理popWindow 显示内容
        handleLogic(contentView);
        //创建并显示popWindow
        mCustomPopWindow = new CustomPopWindow.PopupWindowBuilder(this)
                .setView(contentView)
                .create()
                .showAsDropDown(iv_add, -100, 40);
    }

    /**
     * 处理弹出显示内容、点击事件等逻辑
     *
     * @param contentView
     */
    private void handleLogic(View contentView) {
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCustomPopWindow != null) {
                    mCustomPopWindow.dissmiss();
                }
                Intent intent = new Intent();
                switch (v.getId()) {
                    case R.id.ll_scan_code:
                        intent.setClass(MyScheduleActivity.this, AppointmentMeetingActivity.class);
                        break;
                    case R.id.ll_share_img:
                        intent.setClass(MyScheduleActivity.this, NewSetLiveActivity.class);
                        break;
                }
                startActivity(intent);
            }
        };
        contentView.findViewById(R.id.ll_scan_code).setOnClickListener(listener);
        contentView.findViewById(R.id.ll_share_img).setOnClickListener(listener);

    }


    private class MyAdapter extends FragmentStatePagerAdapter {
        private List<Fragment> mFragments;

        public MyAdapter(FragmentManager fm, List<Fragment> mFragments) {
            super(fm);
            this.mFragments = mFragments;
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = null;
            fragment = mFragments.get(position);
            return fragment;
        }

        @Override
        public int getCount() {
            return mDataList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mDataList.get(position);
        }
    }
}
